﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using FaWorksSolution.MvcApplication.Models.Assets;

namespace FaWorksSolution.MvcApplication.API.Controllers
{
    public class GroupController : ApiControllerBase
    {
        // GET api/Group
        public IEnumerable<Group> GetGroups()
        {
            return db.AssetsGroups.Include("Files.Cdns").OrderBy(m => m.GroupName).AsEnumerable();
        }

        // GET api/Group/5
        public Group GetGroup(Guid id)
        {
            Group group = db.AssetsGroups.Find(id);
            if (group == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return group;
        }

        // PUT api/Group/5
        public HttpResponseMessage PutGroup(Guid id, Group group)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != group.GroupId)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(group).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/Group
        public HttpResponseMessage PostGroup(Group group)
        {
            if (ModelState.IsValid)
            {
                group.GroupId = Guid.NewGuid();
                db.AssetsGroups.Add(group);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, group);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = group.GroupId }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }

        // DELETE api/Group/5
        public HttpResponseMessage DeleteGroup(Guid id)
        {
            Group group = db.AssetsGroups.Include("Files.Cdns").FirstOrDefault(m => m.GroupId == id);
            if (group == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.AssetsGroups.Remove(group);
            foreach (var file in group.Files)
            {
                db.AssetsFiles.Remove(file);
                foreach (var cdn in file.Cdns)
                {
                    db.AssetsCdns.Remove(cdn);
                }
            }

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, group);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        private DefaultContext db = new DefaultContext();
    }
}